package vos.vn.custom;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.RatingBar;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class CustomRating extends RatingBar implements OnTouchListener {	
	public CustomRating(Context context) {
		super(context);
		setOnTouchListener(this);
	}
	
	public CustomRating(Context context, AttributeSet attrs) {
		super(context, attrs);
		setOnTouchListener(this);
	}
	
	public CustomRating(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		setOnTouchListener(this);
	}

	@Override
	public boolean onTouch(View v, MotionEvent ev) {
		final int action = ev.getAction();
		
        switch (action) {
        case MotionEvent.ACTION_DOWN: 
            final float x = ev.getX();
            float d = this.getRight() - this.getLeft();
            float dx = x - this.getLeft();
            float vote = (float) ((dx/d) * this.getNumStars() + 0.5);
            this.setRating(vote);            
            break;            
        default:
        	break;
        }
        
        return true;
	}

}
